package com.inspur.edp.bef.bemanager.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bef.bizentity.exceptions.BusinessEntityErrorCodeEnum;
import com.inspur.edp.bef.bizentity.exceptions.BusinessEntityException;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.common.Utils;
import com.inspur.edp.metadata.rtcustomization.spi.MetadataBeforeSaveToDBExtend;
import com.inspur.edp.bef.bemanager.ContentSerializer;


public class SimplifyBeBeforeSavedToDB implements MetadataBeforeSaveToDBExtend {

    public String execute(GspMetadata gspMetadata){
        ContentSerializer serializer = new ContentSerializer();
        JsonNode jsnode = serializer.Serialize(gspMetadata.getContent());
        ObjectMapper mapper = Utils.getMapper();
        String mdValue;
        try {
        mdValue = mapper.writeValueAsString(jsnode);
        }catch (JsonProcessingException e){
            throw BusinessEntityException.createException(BusinessEntityErrorCodeEnum.GSP_BEMODEL_JSON_0005, e, "Metadata" , gspMetadata.getHeader().getId());
        }
        return mdValue;
    }

}
